﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using SQLExecute;
using DevExpress.Web.ASPxGridView;


/// <summary>
/// Summary description for NghiPhep
/// </summary>
public class NghiPhep
{
    private DataSet ds;
    private int _id;
    public int ID
    {
        set { _id = value; }
        get { return _id; }
    }
    private string _email;
    public string Email
    {
        set { _email = value; }
        get { return _email; }
    }  
    private DateTime _nghitungay;
    public DateTime NghiTuNgay
    {
        set { _nghitungay = value; }
        get { return _nghitungay; }
    }
    private DateTime _nghidenngay;
    public DateTime NghiDenNgay
    {
        set { _nghidenngay = value; }
        get { return _nghidenngay; }
    }
    private string _thoigiannghi;
    public string ThoiGianNghi
    {
        set { _thoigiannghi = value; }
        get { return _thoigiannghi; }
    }
    private string _loaiphep;
    public string LoaiPhep
    {
        set { _loaiphep = value; }
        get { return _loaiphep; }
    }
    private string _lydo;
    public string LyDo
    {
        set { _lydo = value;  }
        get { return _lydo; }
    }
    private int _phongbanid;
    public int MaPhongBan
    {
        set { _phongbanid = value; }
        get { return _phongbanid; }
    }


    public NghiPhep()
    { }
	public NghiPhep(string email,int phongbanid, DateTime from,DateTime to
                    ,string thoigiannghi,string loaiphep,string lydo)
	{

        this._email = email;
        this._phongbanid = phongbanid;
        this._nghitungay = from;
        this._nghidenngay = to;
        this._thoigiannghi = thoigiannghi;
        this._loaiphep = loaiphep;
        this._lydo=lydo;

	}
    public void Insert_Nghi_Phep()
    {
        SqlHelper.ExecuteNonQuery(Common.strConnect, "Insert_Nghi_Phep", 
                                  new object[] {_email,_phongbanid, _nghitungay,_nghidenngay
                                                ,_thoigiannghi,_loaiphep,_lydo
                                                ,DateTime.Now.ToShortDateString() });
    }
    public NghiPhep Get_Info_Nghi_Phep(int id)
    {
        NghiPhep n = new NghiPhep();
        
        SqlDataReader r = SqlHelper.ExecuteReader(Common.strConnect, "Get_Info_Nghi_Phep", new object[]{id});
        if (r.HasRows)
        {
            while (r.Read())
            {
               n.Email = r[0].ToString();
               n.NghiTuNgay =Convert.ToDateTime(r[1].ToString());
               n.NghiDenNgay =Convert.ToDateTime(r[2].ToString());
               n.LoaiPhep = r[3].ToString();
               n.LyDo = r[4].ToString();
               n.ThoiGianNghi = r[5].ToString();
               n.MaPhongBan =Convert.ToInt16(r[6].ToString());

            }
            r.Close();
        }

        return n;
    }
    public void List_Nghi_Phep(ASPxGridView gdNghiPhep, string email)
    {
        ds = SqlHelper.ExecuteDataset(Common.strConnect, "List_Nghi_Phep", new object[] {email });
        if (ds.Tables[0].Rows.Count > 0)
        {
            gdNghiPhep.DataSource = ds;
            gdNghiPhep.DataBind();
        }
    }    
    public void List_Nghi_Phep_Theo_PhongBan(DropDownList cbPhongBan,DevExpress.Web.ASPxGridView.ASPxGridView gd
                                             ,DateTime? dtFrom, DateTime? dtTo)
    {


        ds = SqlHelper.ExecuteDataset(Common.strConnect, "List_Nghi_Phep_Phong_Ban",
            new object[] { Convert.ToInt16(cbPhongBan.SelectedValue), dtFrom, dtTo });
        if (ds.Tables[0].Rows.Count > 0)
        {
            gd.DataSource = ds.Tables[0];
            gd.DataBind();
            
        }

    }

    public void Duyet_Nghi_Phep(int id, int nValue,int group)
    {
        string sql = string.Empty; ;

        //admin update 1
        if (group == 1)
            sql = "update tbl_nghiphep set donvi='" + nValue + "' where id='" + id + "'";
        //manager update 2
        if (group == 2)
            sql = "update tbl_nghiphep set nhansu='" + nValue + "' where id='"+id+"'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);

    }
    public void Chi_Tiet_Nghi_Phep(int id, ASPxGridView gdNghiPhep)
    {
        ds = SqlHelper.ExecuteDataset(Common.strConnect, "List_Nghi_Phep", new object[] { id });
        if (ds.Tables[0].Rows.Count > 0)
        {
            gdNghiPhep.DataSource = ds;
            gdNghiPhep.DataBind();
        }
    }

    public void Tong_Hop_Nghi_Phep_Theo_PhongBan(DropDownList cbPhongBan, ASPxGridView gd, DateTime? from, DateTime? to)
    { 
    
    
    }

    public void Xoa_Nghi_Phep(int id)
    {
        string sql = string.Empty;


        sql = "update tbl_nghiphep set active=0 where id='" + id + "'";
        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);
        
    }
    
}
